<template>
  <div class="setting-main">
    <div class="setting-header">
      <span @click="goBack"><van-icon name="arrow-left" /></span>
      <div class="center">设置资金密码</div>
    </div>
    <div class="setting-box-cell">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item  prop="pass">
          <template slot="label">
            <span class="password-label">资金密码</span>
          </template>
          <el-input placeholder="请输入资金密码" type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item style="margin-top: -15px" prop="checkPass">
          <template slot="label">
            <span class="password-label">再次输入资金密码</span>
          </template>
          <el-input placeholder="再次输入资金密码"  type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="setting-box">
      <van-button style="border-radius: 5px" block @click="submitForm('ruleForm')" color="#002FFF">提交</van-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入资金密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入资金密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: '',
        checkPass: '',

      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    };
  },
  methods:{
    goBack(){
      this.$router.go(-1)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style scoped lang="scss">
.setting-main{
  width: 100%;
  padding:0 3%;
  box-sizing: border-box;
  .setting-header{
    width: 100%;
    height: 45px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    span{
      position: absolute;
      left: 0;
    }
    .center{
      width: 100%;
      height: 45px;
      text-align: center;
      line-height: 45px;
      font-size: 15px;
    }
  }
  .setting-box-cell{
    width: 100%;
  }
  .setting-box{
    margin-top: 40px;
  }
  .password-label{
    font-size: 12px;
  }
}

</style>